package cn.itcast.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.dao.OrderDao;
import cn.itcast.dao.PayLogDao;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.PayLog;
import cn.itcast.service.PayService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {


    @Autowired
    private PayLogDao payLogDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 修改
     *
     * @param result
     */
    @Override
    public void updatePayStatus(Map<String, String> result) {
        //微信订单号	transaction_id
        String transaction_id = result.get("transaction_id"); //微信支付交易号
        String orderId = result.get("out_trade_no");   //商户订单号	out_trade_no
        //根据orderId查找订单号
        Order order = this.orderDao.findById(orderId);
        if (ObjectUtil.isEmpty(order)) {
            throw new RuntimeException("未查找到订单信息,我的小主!");
        }
        order.setOrderStatus(1);
        order.setPayStatus(1);
        this.orderDao.updata(order);

        //根据id 与交易号 查询交易日志
        PayLog payLog = this.payLogDao.findById(orderId);
        if (ObjectUtil.isEmpty(payLog)) {
            //如果为null
            payLog = new PayLog();
            payLog.setOrderId(orderId);
            payLog.setCreateTime(new Date());
            payLog.setMoney(Double.parseDouble(result.get("total_fee")));
        }
        //有该信息.,说明已经支付
        payLog.setPayStatus(1);
        payLog.setTransactionId(transaction_id);
        payLog.setPayTime(new Date());
        //跟新payLog表信息
        this.payLogDao.update(payLog);
    }
}
